package com.nifostasky.comic;
/**
 *
 * 看漫画的页面
 */

import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.nifostasky.entity.ComicInfo;
import com.nifostasky.entity.UserInfo;
import com.nifostasky.fragment.MainFragment1;
import com.nifostasky.utilDao.SettingInfo;
import com.nifostasky.utils.JsoupContent;

import java.net.URI;

public class ComicPicture extends AppCompatActivity {
    //声明控件
    private LinearLayout mll_comicButton;
    private ImageView miv_picture;
    private Button mbtn_nextPage;
    private Button mbtn_lastPage;
    private Button mbtn_nextChapter;
    private Button mbtn_lastChapter;
    private TextView mtv_text;
    //声明爬取数据的对象
    private JsoupContent jsoupContent = null ;
    //漫画信息
    private ComicInfo comicInfo= null;
    //保存用户当前观看进度
    private UserInfo userInfo = null;
    //当前页面的ChapterUrl
    private String chapterUrl = null;
    //当前页面的图片pictureUrl
    private String pictureUrl = null;
    //判断线程是否以及执行结束
    private boolean isPicture = false;
    //声明设置数据库
    private SettingInfo set  = null;
    //设置的数组
    private int setContext[] = null;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comic_picture);

        //获取控件
        miv_picture = findViewById(R.id.iv_picture);
        mbtn_lastPage = findViewById(R.id.btn_comic1);
        mbtn_lastChapter = findViewById(R.id.btn_comic2);
        mbtn_nextChapter = findViewById(R.id.btn_comic3);
        mbtn_nextPage = findViewById(R.id.btn_comic4);
        mll_comicButton = findViewById(R.id.ll_comicButton);
        mtv_text = findViewById(R.id.tv_text);
        //得到数据库
        set = new SettingInfo(ComicPicture.this);
        //得到内容
        setContext = set.queryShowKeyAndVolumePage();



        //实例化用户观看进度信息 和 爬取网页内容的类
        userInfo = new UserInfo();
        jsoupContent = new JsoupContent();


        //设置按钮的显示
        this.showButton();

        //获取上一个页面的数据
        getIntentInfo(getIntent());
        //显示图片
        setPicture();
        //按键监听事件
        setListeners();
    }





    //获取某章节的所有页数的线程
    private void pictureRunnable(){
        Runnable pictureRunnable = new Runnable() {
            @Override
            public void run() {
                System.out.println( "执行1"+chapterUrl);
                //将标志设置为false 表示获取失败
                ComicPicture.this.jsoupContent.setFlag(false);
                //设置章节的ChapterUrl
                ComicPicture.this.jsoupContent.setUrl(ComicPicture.this.chapterUrl);
                //根据章节的url获取章节内的图片
                ComicPicture.this.jsoupContent.setPictureName();
                //将信息提取出来
                ComicPicture.this.comicInfo.setPictureName(ComicPicture.this.jsoupContent.getComicinfo().getPictureName());
                ComicPicture.this.comicInfo.setPageNumber(ComicPicture.this.jsoupContent.getComicinfo().getPageNumber());
                ComicPicture.this.comicInfo.setUrlAgo(ComicPicture.this.jsoupContent.getComicinfo().getUrlAgo());
                ComicPicture.this.comicInfo.setUrlBefor(ComicPicture.this.jsoupContent.getComicinfo().getUrlBefor());
                ComicPicture.this.isPicture = ComicPicture.this.jsoupContent.isFlag();
                System.out.println( "执行2");
            }
        };
        new Thread(pictureRunnable).start();
        System.out.println( "执行3");
    }



    //将上一个页面的数据获得到并封装
    private void getIntentInfo(Intent intent){

        comicInfo = (ComicInfo) intent.getSerializableExtra("comicInfo");
        userInfo.setCurrentChapter(intent.getIntExtra("position",-1));
        //将默认页数设置为第一页 0 章节链接给设置好
        userInfo.setCurrentPage(0);
        userInfo.setCurrentUrl(comicInfo.getOfficialWebsiteUrl()
                + comicInfo.getChapterName().get(userInfo.getCurrentChapter())[1]);

    }





    private void setPicture(){
        //取得chapterurl
        chapterUrl = userInfo.getCurrentUrl();
        System.out.println(chapterUrl);

        //执行线程获取图片
        pictureRunnable();
        while (true) {
            System.out.println(isPicture);
            if(isPicture) {
                //更新文字内容
                ComicPicture.this.setText();
                //取得pictureurl
                pictureUrl = comicInfo.getUrlAgo()
                        + comicInfo.getUrlBefor()
                        + comicInfo.getPictureName()[userInfo.getCurrentPage()];
                System.out.println(pictureUrl);
                //向图片控件中插入图片
                Glide.with(this).load(pictureUrl).into(miv_picture);
                break;
            }
        }

    }



    //设置按键点击事件
    private void setListeners(){
        ComicPicture.Listener listener = new ComicPicture.Listener();
        mbtn_nextPage.setOnClickListener(listener);
        mbtn_nextChapter.setOnClickListener(listener);
        mbtn_lastPage.setOnClickListener(listener);
        mbtn_lastChapter.setOnClickListener(listener);
    }


    //点击事件实现
    private class Listener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            int newpage;
            // 按键事件定义
            switch (v.getId()){
                case R.id.btn_comic1:
                    System.out.println("按键被点击1");
                    newpage = userInfo.getCurrentPage();
                    if(newpage > 0) {
                        userInfo.setCurrentPage(newpage - 1);
                        //显示图片
                        setPicture();
                    }else{
                        Toast.makeText(ComicPicture.this, "已经是第一页了", Toast.LENGTH_SHORT).show();;
                    }
                    break;
                case R.id.btn_comic2:
                    System.out.println("按键被点击2");
                    newpage = userInfo.getCurrentChapter();
                    if(newpage >0){
                        System.out.println("目前章节数为"+newpage);
                        userInfo.setCurrentChapter(newpage - 1);
                        userInfo.setCurrentUrl(comicInfo.getOfficialWebsiteUrl()
                                + comicInfo.getChapterName().get(newpage - 1)[1]);
                        userInfo.setCurrentPage(0);
                        //初始化线程
                        isPicture = false;
                        //显示图片
                        setPicture();
                    }else{
                        Toast.makeText(ComicPicture.this, "已经是第一章了", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.btn_comic3:
                    System.out.println("按键被点击3");
                    newpage = userInfo.getCurrentChapter();
                    userInfo.setCurrentChapter(newpage + 1);
                    if(newpage < comicInfo.getChapterNumber() - 1){
                        userInfo.setCurrentUrl(comicInfo.getOfficialWebsiteUrl()
                                + comicInfo.getChapterName().get(newpage + 1)[1]);
                        userInfo.setCurrentPage(0);
                        //初始化线程
                        isPicture = false;
                        //显示图片
                        setPicture();
                    }else{
                        System.out.println("章节总页数"+comicInfo.getChapterNumber());
                        Toast.makeText(ComicPicture.this, "已经是最后一章了", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.btn_comic4:
                    System.out.println("按键被点击4");
                    newpage = userInfo.getCurrentPage();
                    if(newpage < comicInfo.getPageNumber() - 1) {
                        userInfo.setCurrentPage(newpage + 1);
                        //显示图片
                        setPicture();
                    }else{
                        Toast.makeText(ComicPicture.this, "已经是最后一页了", Toast.LENGTH_SHORT).show();;
                    }
                    break;
            }
            //更新文字内容
            ComicPicture.this.setText();
        }
    }


    //获取音量键

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        int newpage = -1;
        if(setContext[1] == 1) {
            switch (keyCode) {
                //音量上键
                case KeyEvent.KEYCODE_VOLUME_UP:
                    //禁用减少音量
                    System.out.println("按键被点击1");
                    newpage = userInfo.getCurrentPage();
                    if (newpage > 0) {
                        userInfo.setCurrentPage(newpage - 1);
                        //显示图片
                        setPicture();
                    } else {
                        Toast.makeText(ComicPicture.this, "已经是第一页了,进入上一章", Toast.LENGTH_SHORT).show();
                        newpage = userInfo.getCurrentChapter();
                        if(newpage >0){
                            System.out.println("目前章节数为"+newpage);
                            userInfo.setCurrentChapter(newpage - 1);
                            userInfo.setCurrentUrl(comicInfo.getOfficialWebsiteUrl()
                                    + comicInfo.getChapterName().get(newpage - 1)[1]);
                            userInfo.setCurrentPage(0);
                            //初始化线程
                            isPicture = false;
                            //显示图片
                            setPicture();
                        }else{
                            Toast.makeText(ComicPicture.this, "已经是第一章了", Toast.LENGTH_SHORT).show();
                        }
                    }
                    //更新文字内容
                    this.setText();
                    return true;
                //音量下键
                case KeyEvent.KEYCODE_VOLUME_DOWN:
                    //禁用音量下键
                    System.out.println("按键被点击4");
                    newpage = userInfo.getCurrentPage();
                    if (newpage < comicInfo.getPageNumber() - 1) {
                        userInfo.setCurrentPage(newpage + 1);
                        //显示图片
                        setPicture();
                    } else {
                        Toast.makeText(ComicPicture.this, "已经是最后一页了,进入下一章", Toast.LENGTH_SHORT).show();
                        newpage = userInfo.getCurrentChapter();
                        userInfo.setCurrentChapter(newpage + 1);
                        if(newpage < comicInfo.getChapterNumber() - 1){
                            userInfo.setCurrentUrl(comicInfo.getOfficialWebsiteUrl()
                                    + comicInfo.getChapterName().get(newpage + 1)[1]);
                            userInfo.setCurrentPage(0);
                            //初始化线程
                            isPicture = false;
                            //显示图片
                            setPicture();
                        }else{
                            System.out.println("章节总页数"+comicInfo.getChapterNumber());
                            Toast.makeText(ComicPicture.this, "已经是最后一章了", Toast.LENGTH_SHORT).show();
                        }
                    }
                    //更新文字内容
                    this.setText();
                    return true;
            }
            return super.onKeyDown(keyCode, event);
        }else {
            return super.onKeyDown(keyCode, event);
        }
    }



    /**
     * 设置是否需要显示下方的按钮
     * layout_weight
     */
    private void showButton(){
        if(setContext[0] == 1){
            mll_comicButton.setVisibility(View.GONE);
        }else{
            //空操作
        }
    }



    //显示当前的页数和章节(序号)
    private void setText(){
        String text = (userInfo.getCurrentChapter()+1) + "序 "+
                (userInfo.getCurrentPage()+1) +"\\" +comicInfo.getPageNumber();
        mtv_text.setText(text);
    }


/*
    //获取章节的线程
    private void chapterRunnable(){
        Runnable pictureRunnable = new Runnable() {
            @Override
            public void run() {

            }
        };
        new Thread(pictureRunnable).start();
    }
*/

}
